package com.example.administrator.waitan.mine;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.administrator.waitan.R;
import com.example.administrator.waitan.login.LoginActivity;
import com.example.administrator.waitan.main.MainActivity;
import com.example.administrator.waitan.mine.history.HistoryActivity;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;

/**
 * Created by Administrator on 2016/1/11.
 */
public class MineFragment extends Fragment {
    private Context context;
    private String id;
    private String name;
    private String imageUrl;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine,null);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ShareSDK.initSDK(getActivity());
        getActivity().findViewById(R.id.btn_mine_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_login, null);
                builder.setView(dialogView);
                builder.setTitle("Hey   : ");
                dialogView.findViewById(R.id.btn_dialog_login_sina).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        PlatformActionListener listener = new PlatformActionListener() {
                            @Override
                            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                                //授权成功
                                id = platform.getDb().getUserId();
                                name = platform.getDb().getUserName();
                                imageUrl = platform.getDb().getUserIcon();
                                Log.d("message", "id:" + id + "name:" + name + "url:" + imageUrl);
                                Log.d("hashmap=======", "" + hashMap);
                                Toast.makeText(getActivity(), "登陆成功", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onError(Platform platform, int i, Throwable throwable) {
                                //授权失败
                            }

                            @Override
                            public void onCancel(Platform platform, int i) {
                                //取消授权
                            }
                        };
                        Platform sina = ShareSDK.getPlatform(getActivity(), SinaWeibo.NAME);
                        sina.setPlatformActionListener(listener);
                        sina.showUser(null);
                        sina.authorize();
                    }
                });
                dialogView.findViewById(R.id.btn_dialog_mineMessage).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent goMine = new Intent(getActivity(), MineMessageActivity.class);
                        goMine.putExtra("id", id);
                        goMine.putExtra("name", name);
                        goMine.putExtra("url", imageUrl);
                        context.startActivity(goMine);
                    }
                });
                dialogView.findViewById(R.id.btn_dialog_login).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent goLogin = new Intent(context, LoginActivity.class);
                        context.startActivity(goLogin);
                        Toast.makeText(getActivity(), "前往登陆页面", Toast.LENGTH_SHORT).show();
                    }
                });

                builder.show();
            }
        });
        getActivity().findViewById(R.id.btn_mine_look).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent goMain = new Intent(context, MainActivity.class);
                context.startActivity(goMain);
            }
        });
        getActivity().findViewById(R.id.btn_mine_temps).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent goBmi = new Intent(context, BmiActivity.class);
                context.startActivity(goBmi);
            }
        });
        getActivity().findViewById(R.id.btn_mine_mine).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent goHistoty = new Intent(context,HistoryActivity.class);
                context.startActivity(goHistoty);
            }
        });
    }

}
